﻿using BLL;
using Model;
using System;
using Thksoft.Modal.Page;

namespace Thksoft.BusinessExpansion.Business
{
    public partial class OptinionEdit : FormPage//BaseUI.BasePage
    {
        private Sys_OptinionBLL optinionBLL = new Sys_OptinionBLL();

        protected override void BindPage()
        {
            //var id = Request.QueryString["ID"];
            //var entity = optinionBLL.Find(Convert.ToDecimal(FORM_KEY));
            if (FORM_IFEDIT)
            {
                #region 加载数据

                var entity = optinionBLL.Find(Convert.ToDecimal(FORM_KEY));
                if (entity.N_USER_ID != CurrentUserID)
                {
                    MessageBox.Show(this, "【温馨提示】没有权限");
                    Response.Redirect("/BusinessExpansion/Business/Optinion.aspx");
                    return;
                }
                FORM_ID.Text = entity.N_OPIN_ID.ToString().Trim();
                FORM_VALUE.Value = entity.C_OPIN_NAME;
                FORM_STATE.SelectedValue = entity.N_STATE.ToString().Trim();
                FORM_USER.Text = CurrentUserName;
                FORM_TIME.Text = entity.D_OPIN_TIME.ToString("yyyy-MM-dd HH:mm:ss");

                #endregion 加载数据
            }
            else
            {
                myid.Style.Add("display", "none");
                FORM_ID.Text = "0";
                FORM_USER.Text = CurrentUserName;
                FORM_TIME.Text = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                FORM_VALUE.Value = "";
            }
        }

        protected override void SaveClick(object sender, EventArgs e)
        {
            if (FORM_IFEDIT)
            {
                #region 修改

                try
                {
                    var entity = optinionBLL.Find(Convert.ToDecimal(FORM_ID.Text));
                    if (entity == null)
                    {
                        MessageBox.Show(this, "【温馨提示】数据不存在");
                        return;
                    }
                    if (entity.N_USER_ID != CurrentUserID)
                    {
                        MessageBox.Show(this, "【温馨提示】没有权限");
                        return;
                    }
                    entity.C_OPIN_NAME = FORM_VALUE.Value;
                    entity.N_STATE = Convert.ToDecimal(FORM_STATE.SelectedValue);
                    if (optinionBLL.Update(entity, CurrentUserName) > 0)
                    {
                        MessageBox.Show(this, "【温馨提示】修改成功");
                        return;
                    }
                    else
                    {
                        MessageBox.Show(this, "【温馨提示】修改失败");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "【温馨提示】异常" + ex.Message);
                }

                #endregion 修改
            }
            else
            {
                #region 新增

                try
                {
                    var entity = new Sys_Optinion
                    {
                        N_USER_ID = CurrentUserID,
                        C_OPIN_NAME = FORM_VALUE.Value,
                        D_OPIN_TIME = Convert.ToDateTime(FORM_TIME.Text),
                        N_ISDEL = 0,
                        N_STATE = Convert.ToDecimal(FORM_STATE.SelectedValue)
                    };
                    if (optinionBLL.Insert(entity, CurrentUserName) > 0)
                    {
                        #region 重新加载数据

                        //FORM_IF_EDIT.Value = "TRUE";
                        ////btnSave.Text = "保存";
                        //if (entity.N_USER_ID != CurrentUserID)
                        //{
                        //    MessageBox.Show(this, "【温馨提示】没有权限");
                        //    Response.Redirect("/BusinessExpansion/Business/Optinion.aspx");
                        //    return;
                        //}
                        //FORM_ID.Text = entity.N_OPIN_ID.ToString().Trim();
                        //FORM_VALUE.Value = entity.C_OPIN_NAME;
                        //FORM_STATE.SelectedValue = entity.N_STATE.ToString().Trim();
                        //FORM_USER.Text = CurrentUserName;
                        //FORM_TIME.Text = entity.D_OPIN_TIME.ToString("yyyy-MM-dd HH:mm:ss");

                        #endregion 重新加载数据

                        //MessageBox.Show(this, "【温馨提示】添加成功");
                        MessageBox.ShowAndRedirect(this, "新增成功", "/BusinessExpansion/Business/OptinionEdit.aspx?IF_EDIT=FALSE");
                        //return;
                    }
                    else
                    {
                        MessageBox.Show(this, "【温馨提示】添加失败");
                        return;
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(this, "【温馨提示】异常" + ex.Message);
                }

                #endregion 新增
            }
        }

        protected override void UpdateFormSize()
        {
            if (FORM_IFEDIT)
            {
                MessageBox.ResponseScript(this, "FORM_SIZE(1036,500);");
            }
            else
            {
                MessageBox.ResponseScript(this, "FORM_SIZE(1036,466);");
            }
        }
    }
}